<template>
    <div class="table">
        <el-breadcrumb separator="/" class="breadCrumb">
            <el-breadcrumb-item>{{$t('sidebar.hostMsg')}}</el-breadcrumb-item>
            <el-breadcrumb-item>{{$t('sidebar.taskHostConfig')}}</el-breadcrumb-item>
        </el-breadcrumb>
        <div class="crumbs">
            <el-row :gutter="15">
                <el-col :span="2" style="min-width:100px;">
                    <el-input
                        :placeholder="$t('taskHostConfig.pleaseInput',{ name: 'ID' })"
                        v-model="searchForm.id">
                    </el-input>
                </el-col>
                <el-col :span="4" style="line-height: 35px;">
                    <el-button size="small" type="primary" @click="searchData()">{{$t('taskHostConfig.search')}}</el-button>
                    <el-button size="small" type="primary" @click="refreshData()">{{$t('taskHostConfig.refresh')}}</el-button>
                </el-col>
                <el-col :span="1" class="floatRight">
                    <el-button size="small" type="primary" @click="addButton()">{{$t('taskHostConfig.add')}}</el-button>
                </el-col>
            </el-row>
        </div>
        <el-table :data="tableData" border style="width: 100%" :max-height="$store.state.allTable.tableHeight" v-loading="tableDataLoading"
                  element-loading-text="Loading...">
            <el-table-column prop="id" label="ID" width="80">
            </el-table-column>
            <el-table-column prop="configName" :label="$t('taskHostConfig.configName')" min-width="150">
            </el-table-column>
            <el-table-column prop="configDesc" :label="$t('taskHostConfig.configDesc')"  min-width="200">
            </el-table-column>
            <el-table-column prop="ipStart" :label="$t('taskHostConfig.ipStart')" min-width="200">
            </el-table-column>
            <el-table-column prop="ipEnd" :label="$t('taskHostConfig.ipEnd')" min-width="200">
            </el-table-column>

            <el-table-column prop="runType" :label="$t('taskHostConfig.runType')" min-width="150" :formatter="runTypeShow">
            </el-table-column>
            <el-table-column prop="createDate" :label="$t('taskHostConfig.createDate')" min-width="175">
            </el-table-column>
            <el-table-column prop="modifyDate" :label="$t('taskHostConfig.modifyDate')" min-width="175">
            </el-table-column>
            <el-table-column prop="status" :label="$t('taskHostConfig.status')" min-width="80" :formatter="statusShow">
            </el-table-column>

            <el-table-column :label="$t('taskHostConfig.operation')" min-width="162">
                <template scope="scope">
                    <el-button size="mini"
                               @click="handleEdit(scope)">{{$t('taskHostConfig.edit')}}</el-button>
                    <el-button size="mini" type="danger"
                               @click="deleteData(scope.row.id)">{{$t('taskHostConfig.delete')}}</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="mgtb20" :class="{ pagination : tableData.length !== 0}">
            <el-pagination
                @size-change="handleSizeChange"
                @current-change ="handleCurrentChange"
                :page-sizes="$store.state.allTable.pageSizesChoice"
                :page-size="pageSizeGetter"
                layout="total, sizes, prev, pager, next, jumper"
                :total="sizeAll"
                :page-count="pageCount"
                v-if="tableData.length !== 0">
            </el-pagination>

        </div>
        <el-dialog :title="$t('sidebar.taskHostConfig')" size="small" v-model="dialogFormVisible">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" :inline="true" label-position="right">
                <el-row type="flex">
                    <el-col>
                        <el-form-item :label="$t('taskHostConfig.configName')" prop="configName">
                            <el-input v-model="ruleForm.configName" :placeholder="$t('taskHostConfig.configName')"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item :label="$t('taskHostConfig.configDesc')" prop="configDesc">
                            <el-input v-model="ruleForm.configDesc" :placeholder="$t('taskHostConfig.configDesc')"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row type="flex">
                    <el-col>
                        <el-form-item :label="$t('taskHostConfig.ipStart')" prop="ipStart">
                            <el-input v-model="ruleForm.ipStart" :placeholder="$t('taskHostConfig.ipStart')" autoComplete="off"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item :label="$t('taskHostConfig.ipEnd')" prop="ipEnd">
                            <el-input v-model="ruleForm.ipEnd" :placeholder="$t('taskHostConfig.ipEnd')" autoComplete="off"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row type="flex">
                    <el-col>
                        <el-form-item :label="$t('taskHostConfig.runType')" prop="runType">
                            <!--<el-input v-model="ruleForm.runType" :placeholder="$t('taskHostConfig.runType')"></el-input>-->
                            <el-select v-model="ruleForm.runType" :placeholder="$t('taskHostConfig.runType')">
                                <el-option
                                    key="1"
                                    label="全局唯一"
                                    value="1">
                                </el-option>
                                <el-option
                                    key="0"
                                    label="任意执行"
                                    value="0">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col></el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">{{$t('taskHostConfig.cancel')}}</el-button>
                <el-button type="primary" @click="addData('ruleForm')">{{$t('taskHostConfig.confirm')}}</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
    import { mapGetters,mapMutations } from 'vuex';
    import common from '../../assets/script/common';
    import { isIP } from '../../assets/script/validate';
    export default {
        data() {
            const validateIp = (rule, value, callback) => {
                if (!isIP(value)) {
                    callback(new Error('请输入正确的IP地址'));
                } else {
                    callback();
                }
            };
            return {
                tableData:[],
                pageName:'taskHostConfig',
                pageCount:1,
                sizeAll:0,
                dialogFormVisible: false,
                searchForm:{
                    id:''
                },
                ruleForm: {
                    id:'',
                    configName: '',
                    configDesc: '',
                    createDate: '',
                    ipStart: '',
                    ipEnd:'',
                    modifyDate:'',
                    runType:'',
                    status:''
                },
                rules: {
                    configName: [
                        { required: true, message: this.$i18n.t('taskHostConfig.pleaseInput',{ name: this.$i18n.t('taskHostConfig.configName') }), trigger: 'blur' }
                    ],
                    configDesc: [
                        { required: true, message: this.$i18n.t('taskHostConfig.pleaseInput',{ name: this.$i18n.t('taskHostConfig.configDesc') }), trigger: 'blur' }
                    ],
                    ipStart: [
                        { required: true, message: this.$i18n.t('taskHostConfig.pleaseInput',{ name: this.$i18n.t('taskHostConfig.ipStart') }), trigger: 'blur' },
                        { required: true,  trigger: 'blur', validator: validateIp }
                    ],
                    ipEnd: [
                        { required: true, message: this.$i18n.t('taskHostConfig.pleaseInput',{ name: this.$i18n.t('taskHostConfig.ipEnd') }), trigger: 'blur' },
                        { required: true,  trigger: 'blur', validator: validateIp }

                    ],
                    modifyDate: [
                        { required: true, message: this.$i18n.t('taskHostConfig.pleaseInput',{ name: this.$i18n.t('taskHostConfig.modifyDate') }), trigger: 'blur' }
                    ],
                    runType: [
                        { required: true, message: this.$i18n.t('taskHostConfig.pleaseInput',{ name: this.$i18n.t('taskHostConfig.runType') }), trigger: 'blur' }
                    ]
                },
                hasTaskClass: true,
                dialogType:'',
                searchId:'',
                tableDataLoading:true
            }
        },
        created() {
            this.getData(1,this.$store.state.allTable.pageSize);
        },
        computed:{
            ...mapGetters([
                "pageSizeGetter",
            ]),
        },
        methods:{
            ...mapMutations([
                'pageSizeMutation'
            ]),
            handleSizeChange(val) {
                let self = this;
                self.pageSizeMutation(val);
                self.getData(1,self.$store.state.allTable.pageSize);
            },
            handleCurrentChange(val) {
                common.changePageGoToTop(this,val);
            },
            getData(_pageSize,_pageNum) {
                let self = this;
                self.tableDataLoading = true;
                common.getTableList(this, 'taskHostConfig', _pageSize, _pageNum, self.searchForm, (data) => {
                    self.tableData = data.results ? data.results : [];
                    self.pageCount = data.pageCount;
                    self.sizeAll = data.sizeAll;
                    self.tableDataLoading = false;
                });
            },
            addData(formName) {
                let self = this;
                let __token = sessionStorage.getItem("token");
                let __ruleForm = self.ruleForm;

                this.$refs[formName].validate((valid) => {
                    if(valid){
                        self.ruleForm.ipStart = common.ip2int(self.ruleForm.ipStart);
                        self.ruleForm.ipEnd = common.ip2int(self.ruleForm.ipEnd);
                        if(this.dialogType === 'add'){
                            common.postTableList(self, 'taskHostConfig', __ruleForm, () => {
                                self.getData(1,self.$store.state.allTable.pageSize);
                                self.dialogFormVisible = false;
                                self.hasTaskClass = true;
                                for(let key in __ruleForm){
                                    __ruleForm[key] = '';
                                }
                                self.ruleForm = __ruleForm;
                            });
                        }else if(this.dialogType === 'edit'){
                            common.putTableList(self, 'taskHostConfig', __ruleForm, () => {
                                self.getData(1,self.$store.state.allTable.pageSize);
                                self.dialogFormVisible = false;
                                self.hasTaskClass = true;
                                for(let key in __ruleForm){
                                    __ruleForm[key] = '';
                                }
                                self.ruleForm = __ruleForm;
                            });
                        }

                    }
                });
            },
            deleteData(_id) {
                let self = this;
                common.deleteTableList(self, 'taskHostConfig', _id, () => {
                    self.getData(1,self.$store.state.allTable.pageSize);
                })
            },
            handleEdit(scopes) {
                let __ruleForm = this.ruleForm;
                this.hasTaskClass = false;
                for(let key in __ruleForm){
                    if(scopes.row[key] !== null){
                        __ruleForm[key] = scopes.row[key].toString();
                    }else {
                        __ruleForm[key] = '';
                    }

                }
                this.ruleForm = __ruleForm;
                this.dialogFormVisible = true;
                this.dialogType = 'edit';
            },
            searchData() {
                let self = this;
                self.getData(1,self.$store.state.allTable.pageSize);
            },
            refreshData() {
                let self = this;
                for(let key in self.searchForm){
                    self.searchForm[key] = '';
                }
                self.getData(1,self.$store.state.allTable.pageSize);
            },
            addButton() {
                let __ruleForm = this.ruleForm;
                for(let key in __ruleForm){
                    __ruleForm[key] = '';
                }
                this.dialogFormVisible = true;
                this.dialogType = 'add';
                this.hasTaskClass = true;
            },
            statusShow(row, column) {
                if(row.status === 1)
                    return "正常";
            },
            runTypeShow(row, column) {
                if(row.runType === 1)
                    return "全局唯一";
                else
                    return "任意执行";
            }
        }
    }
</script>
<style lang="stylus">

</style>
